package daobeauty.dao;

import org.hibernate.criterion.Order;

/**
 * Created 16.09.2008
 * @author M. Serhat Cinar
 *
 */
public class Sorting {
	private String property;
	private boolean ascending = true;
	
	/**
	 * @param property
	 */
	public Sorting(String property) {
		super();
		this.property = property;
	}
	
	/**
	 * @param property
	 * @param ascending
	 */
	public Sorting(String property, boolean ascending) {
		super();
		this.property = property;
		this.ascending = ascending;
	}
	/**
	 * @return the ascending
	 */
	public boolean isAscending() {
		return ascending;
	}
	
	/**
	 * @return the property
	 */
	public String getProperty() {
		return property;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + (ascending ? 1231 : 1237);
		result = PRIME * result + ((property == null) ? 0 : property.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (obj == null) return false;
		if (getClass() != obj.getClass()) return false;
		final Sorting other = (Sorting) obj;
		if (ascending != other.ascending) return false;
		if (property == null) {
			if (other.property != null) return false;
		}
		else if (!property.equals(other.property)) return false;
		return true;
	}
	
	public Order getOrder(){
		if (ascending){
			return Order.asc(getProperty());
		}
		else{
			return Order.desc(getProperty());
		}
	}
	
	public String toString(){
		return "ORDER BY "+property+" "+(ascending?"ASC":"DESC");
	}
	
	
	// factory methods
	
	public static Sorting desc(String property){
		return new Sorting(property, false);
	}

	public static Sorting asc(String property){
		return new Sorting(property, true);
	}

}
